#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
import time
from rclpy.qos import QoSProfile, QoSReliabilityPolicy, QoSDurabilityPolicy
class ROS2Subscriber(Node):
    def __init__(self):
        super().__init__('ros2_subscriber')
        qos = QoSProfile(
            reliability=QoSReliabilityPolicy.RELIABLE,  # 修改为 RELIABLE
            durability=QoSDurabilityPolicy.VOLATILE,
            depth=10
        )
        self.subscription = self.create_subscription(
            String, 'throughput_topic', self.callback, qos)
        self.count = 0
        self.start_time = time.time()

    def callback(self, msg):
        self.count += 1
        if self.count % 1000 == 0:
            elapsed = time.time() - self.start_time
            print(f"ROS2 Throughput: {self.count / elapsed:.2f} msgs/sec", flush=True)

def main():
    rclpy.init()
    node = ROS2Subscriber()
    rclpy.spin(node)
    node.destroy_node()
    rclpy.shutdown()

if __name__ == '__main__':
    main()